package com.csq.aop;

import org.aspectj.lang.JoinPoint;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author ercai
 * @date 2019/7/25 - 14:53
 */
public class TransactionAop {

    public void begin(JoinPoint joinPoint){
        System.out.println("前置通知");
        if("insert".equals(joinPoint.getSignature().getName())){
            System.out.println("提交事务");
        }else{
            System.out.println("不提交事务");
        }
    }

    public void after(){
        System.out.println("后置通知");
    }


    public void throwingMethod(JoinPoint joinPoint,Exception ex){

        String method = joinPoint.getSignature().getName();
        Object target = joinPoint.getTarget();
        Object[] args = joinPoint.getArgs();

        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("发生异常的时间是"+simpleDateFormat.format(date));
        System.out.println("在" + method + "方法上发生了：");
        System.out.println(ex.getMessage() + "异常");
        System.out.println("被代理的目标对象：" + target);
        System.out.println("参数列表为：");
        for (Object obj : args) {
            System.out.println(obj);
        }
    }
}
